#!/bin/bash

# author Matej Isteník <matej.istenik@gmail.com>

WIDTH=0
HEIGHT=0

CENTER=0

LEFT_LIST_COUNT=$2
RIGHT_LIST_COUNT=$4

LEFT_LIST=()
RIGHT_LIST=()

SHOW_HELP=$6

SELECTED=$1
SELECTED_LEFT_INDEX=$3
SELECTED_RIGHT_INDEX=$5

WORKING_DIR_LEFT=$7
WORKING_DIR_RIGHT=$8

# $1 left / right
# $2 count
function loadFiles
{
	local LIST
	local TYPE
	local NAME
	local DATE
	local SIZE
	
	for index in `seq 0 ${2}`
	do
		read TYPE NAME DATE SIZE

		LIST[${index}]="${TYPE} ${NAME} ${DATE} ${SIZE}"
	done
	
	case ${1} in
	"left")
		LEFT_LIST=(${LIST[@]})
	;;
	"right")
		RIGHT_LIST=(${LIST[@]})
	;;
	*)
		echo "loadFiles bad parameter"
		exit
	esac
}

loadFiles left $LEFT_LIST_COUNT
loadFiles right $RIGHT_LIST_COUNT

function drawEnvironment
{
	tput civis
	
	let WIDTH=$(tput cols)
	let HEIGHT=$(tput lines)
	
	let CENTER="${WIDTH} / 2"

	local EVEN
	let EVEN="$WIDTH % 2"
	if [ $EVEN -eq 1 ]
	then
		let CENTER+=1
	fi
	
	clearScreen

	drawHeader
	
	drawCenterLine
	
	if [ $SHOW_HELP = "1" ]
	then
		drawHelp
	fi
	
	drawPanel left
	drawPanel right
	
	setFontStyle normal
	
	moveCursor ${HEIGHT} 0
	
	tput cnorm
}

function clearScreen
{
	setFontStyle clearChar

	for i in `seq 0 ${HEIGHT}`
	do
		for j in `seq 0 ${WIDTH}`
		do
			moveCursor $i $j
			echo -n " "
		done
	done
	
	moveCursor 0 0
}

function drawHeader
{
	moveCursor 0 0
	setFontStyle title
	
	local TITLE="BloodyCommander v0.666"
	local TITLE_SIZE=${#TITLE}
	let TITLE_SIZE+=2
	
	local AUTHORS="(c) whileTrueDrink 2011"
	local AUTHORS_SIZE=${#AUTHORS}
	let AUTHORS_SIZE+=2
	
	local LEFT_BORDER
	local RIGHT_BORDER
	
	let LEFT_BORDER="(${CENTER} - ${TITLE_SIZE}) - 4"
	let RIGHT_BORDER="(${CENTER} - ${AUTHORS_SIZE}) - 3"
	
	local LEFT_OFFSET
	local RIGHT_OFFSET
	
	let LEFT_OFFSET="${#WORKING_DIR_LEFT} + 4"
	let RIGHT_OFFSET="${#WORKING_DIR_RIGHT} + 5"
	
	printf \\u2563
	echo -n " ${WORKING_DIR_LEFT} "
	printf \\u2560
	
	for i in `seq ${LEFT_OFFSET} ${LEFT_BORDER}`
	do
		printf \\u2567
	done
	
	printf \\u2563
	echo -n " $TITLE "
	printf \\u2560
	printf \\u2566
	printf \\u2563
	echo -n " $AUTHORS "
	printf \\u2560
	
	for i in `seq ${RIGHT_OFFSET} ${RIGHT_BORDER}`
	do
		printf \\u2567
	done
	
	printf \\u2563
	echo -n " ${WORKING_DIR_RIGHT} "
	printf \\u2560\\u2567
}

function drawCenterLine
{	
	local LINE_HEIGHT
	let LINE_HEIGHT="${HEIGHT} - 1 - ${SHOW_HELP}"
	
	for index in `seq 2 ${LINE_HEIGHT}`
	do
		moveCursor $index $CENTER
		printf \\u2551
	done
}

function drawHelp
{
	local POSITION
	let POSITION="${HEIGHT}-1"
	
	moveCursor $POSITION 0
	
	local KEYS=(^H F2 F3 F4 F5 F6 F7 F8 F9)
	local ACTIONS=(help rename show edit copy move mkdir delete refresh)

	for index in `seq 0 8`
	do
		setFontStyle listCursor
		echo -n "${KEYS[${index}]}"
		setFontStyle listFile
		echo -n " ${ACTIONS[${index}]} "
	done
}

# $1 left / right
function drawPanel
{
	local LIST
	local LIST_COUNT
	
	local SIDE=${1}

	local PANEL_WIDTH
	let PANEL_WIDTH=${WIDTH}/2

	local PANEL_HEIGHT
	let PANEL_HEIGHT="${HEIGHT} - 3 - ${SHOW_HELP}"

	local COLUMN_OFFSET
	local DATE_OFFSET=0
	local SIZE_OFFSET=0
	
	local SELECTED_INDEX
	
	case $SIDE in
	"left")
		LIST=(${LEFT_LIST[@]})
		LIST_COUNT=${LEFT_LIST_COUNT}
		SELECTED_INDEX=$SELECTED_LEFT_INDEX
		COLUMN_OFFSET=0
	;;
	"right")
		LIST=(${RIGHT_LIST[@]})
		LIST_COUNT=${RIGHT_LIST_COUNT}
		SELECTED_INDEX=$SELECTED_RIGHT_INDEX
		COLUMN_OFFSET=$CENTER

	;;
	*)
		echo "drawList bad parameter"
		exit
	esac
	
	let COLUMN_OFFSET+=1

	let DATE_OFFSET="(${PANEL_WIDTH} -19) + ${COLUMN_OFFSET}"
	
	let SIZE_OFFSET="(${PANEL_WIDTH} -7) + ${COLUMN_OFFSET}"
	
	local ITEMS_OFFSET="${SELECTED_LEFT_INDEX} + ${PANEL_HEIGHT} / 2"
	ITEMS_OFFSET=0
	
	local START
	local END
	
	if [ $LIST_COUNT -lt $PANEL_HEIGHT ]
	then
		let START=0
		let END="${LIST_COUNT} - 1"
	else
		let START="${SELECTED_INDEX} - ${PANEL_HEIGHT} / 2"
	
		if [ $START -lt 0 ]
		then
			START=0
		fi
	
		let END="${START} + ${PANEL_HEIGHT}"

		if [ $END -gt $LIST_COUNT ]
		then
			let END="${LIST_COUNT} - 1"
			let START="${LIST_COUNT} - ${PANEL_HEIGHT} - 1"
		fi
	fi
	
	if [ $SELECTED_INDEX -lt 0 ]
	then
		SELECTED_INDEX=0
	fi
	
	if ! [ $SELECTED_INDEX -lt $LIST_COUNT ]
	then
		let SELECTED_INDEX="${LIST_COUNT} - 1"
	fi

	for index in `seq ${START} ${END}`
	do
		local ITEM_POS
		let ITEM_POS="${index} + 2 - ${START}"
		
		local ITEM_INDEX=${index}
		
		let ITEM_INDEX=${ITEM_INDEX}*4
		
		local TYPE=${LIST[${ITEM_INDEX}]}
		
		if [ ! ${TYPE} ]
		then
			break
		fi
		
		let ITEM_INDEX+=1
		local NAME=${LIST[${ITEM_INDEX}]}
		
		let ITEM_INDEX+=1
		local DATE=${LIST[${ITEM_INDEX}]}
		
		let ITEM_INDEX+=1
		local SIZE=${LIST[${ITEM_INDEX}]}
		
		if [ $index -eq ${SELECTED_INDEX} ]
		then
			if [ $index -eq ${SELECTED_INDEX} ] && [ ${SIDE} = ${SELECTED} ]
			then
				drawCursor
			else
				setFontStyle listSelected
			fi
		else
			if [ $TYPE = "d" ]
			then
				setFontStyle listDir
			else
				setFontStyle listFile
			fi
		fi

		local MAX_NAME_SIZE
		let MAX_NAME_SIZE="${DATE_OFFSET} - ${COLUMN_OFFSET}"

		printName $TYPE $NAME $MAX_NAME_SIZE
		printDate $DATE
		printSize $SIZE
		
		tput sgr0
	done
}

function drawCursor
{
	moveCursor $ITEM_POS $COLUMN_OFFSET
	
	setFontStyle listCursor

	local CURSOR_WIDTH
	let CURSOR_WIDTH="${PANEL_WIDTH} - 2"

	for i in `seq 0 ${CURSOR_WIDTH}`
	do
		echo -n " "
	done
}

function printName
{
	moveCursor $ITEM_POS $COLUMN_OFFSET
	
	echo -n "${2} ${3}" | awk '{
	size=$2-1
	name=substr($1, 0, size)
	gsub(":;", " ", name)
	printf " %s", name}'
}

function printDate
{
	moveCursor $ITEM_POS $DATE_OFFSET
	
	local CURRENT_TIME=`date +%s`
	local TIME=${1}
	
	if [ $CURRENT_TIME -lt $TIME ]
	then
		echo -n "in future"
		return
	fi
	
	let TIME="${CURRENT_TIME} - ${TIME}"
	
	local TIME_DIV=(60 60 24 30 12)
	local TIME_TYPE=(sec min hod day month year)
	
	local TIME_TYPE_INDEX=0
	
	for DIV in "${TIME_DIV[@]}"
	do
		if [ $TIME -lt $DIV ]
		then
			echo -n "${TIME} ${TIME_TYPE[${TIME_TYPE_INDEX}]}"
			return
		else
			let TIME_TYPE_INDEX+=1
			let TIME/="${DIV}"
		fi
	done
	
	echo -n "${TIME} ${TIME_TYPE[${TIME_TYPE_INDEX}]}"
}

function printSize
{
	moveCursor $ITEM_POS $SIZE_OFFSET
	
	local SIZE=$1
	local SIZE_TYPES=(B KB MB GB TB)
	
	if [ "$SIZE" = "-" ]
	then
		echo -n $SIZE
		return
	fi
	
	local SIZE_LIMIT=1000;

	for SIZE_TYPE in "${SIZE_TYPES[@]}"
	do
		if [ $SIZE -lt $SIZE_LIMIT ]
		then
			let SIZE/="${SIZE_LIMIT} / 1000"
			echo -n "${SIZE} ${SIZE_TYPE}"
			return
		fi
		
		let SIZE_LIMIT*=1000;
	done
}

# $1 height
# $2 width
function moveCursor
{
	echo -en "\033[${1};${2}f"
}

# $1 predefined style
function setFontStyle
{
	case ${1} in
	"clearChar")
		echo -en "\e[00;33m"
	;;
	"title")
		echo -en "\e[01;31m"
	;;
	"normal")
		echo -en "\e[00;37m"
	;;
	"listDir")
		echo -en "\e[01;37m"
	;;
	"listFile")
		echo -en "\e[00;33m"
	;;
	"listSelected")
		echo -en "\e[01;31m"
	;;
	"listCursor")
		echo -en "\e[01;31;47m"
	;;
	*)
		echo "setFontColor bad parameter"
		exit
	esac
}

# rozhranie

function setFilesInPanel
{
	clear
	# $1 panel (left, right)
	# $2 pole stringov (ak ho dokazes poslat)
}

function setActiveFile
{
	clear
	# $1 panel (left, right)
	# $2 index
	# logiku ci sa scrollne alebo nescrollne je na logike funkcie
}

drawEnvironment
